﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyQQ
{
    public partial class Frm_Login : Form
    {
        public Frm_Login()
        {
            InitializeComponent();
        }
        //调用DataOperator类实现用户登录
        DataOperator dataOper = new DataOperator();

        //创建验证登录方法
        public bool ValidateInput()
        {
            if (txtID.Text.Trim() == "")                                                //登录账号
            {
                MessageBox.Show("请输入登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtID.Focus();                                                           //使登录账号文本框获得鼠标焦点
                return false;
            }
            else if (int.Parse(txtID.Text.Trim()) > 65535)
            {
                MessageBox.Show("请输入正确的登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtID.Focus();                                                           //使登录账号文本框获得鼠标焦点
                return false;
            }
            else if (txtID.Text.Length > 5 && txtPwd.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPwd.Focus();                                                         //使密码文本框获得鼠标焦点
                return false;
            }
            return true;
        }

        //设置ID文本框的按键方式
        private void txtID_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果按键是数字，或者回车，或者删除键
            if (char.IsDigit(e.KeyChar) || (e.KeyChar == '\r') || (e.KeyChar == '\b'))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        //"安全登录"点击事件
        private void pboxLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInput())                             //调用自定义方法验证用户输入
            {
                //因为数据库的表tb_User中 Pwd 定义的类型是字符串类型，所以要在 " + txtPwd.Text.Trim() + " 两侧加上单引号   '" + txtPwd.Text.Trim() + " '
                string sql = "select count(*) from tb_User where ID=" + int.Parse(txtID.Text.Trim()) + "and Pwd= '" + txtPwd.Text.Trim() + " '";
                int num = dataOper.ExecSQL(sql);
                if (num == 1)  //验证通过
                {
                    PublicClass.loginID = int.Parse(txtID.Text.Trim());//设置登录的用户ID
                    if (cboxRemember.Checked)
                    {
                        dataOper.ExecSQLResult("update tb_User set Remember=1 where ID=" + int.Parse(txtID.Text.Trim()));//记住密码
                        if (cboxAutoLogin.Checked)
                        {
                            dataOper.ExecSQLResult("update tb_User set AutoLogin=1 where ID=" + int.Parse(txtID.Text.Trim()));//自动登录
                        }
                    }
                    else
                    {
                        dataOper.ExecSQLResult("update tb_User set Remember=0 where ID=" + int.Parse(txtID.Text.Trim()));
                        dataOper.ExecSQLResult("update tb_User set AutoLogin=0 where ID=" + int.Parse(txtID.Text.Trim()));
                    }
                    //设置在线状态
                    dataOper.ExecSQLResult("update tb_User set Flag=1 where ID=" + int.Parse(txtID.Text.Trim()));
                    //点击后跳转到新建窗体 Frm_Main
                    Frm_Main frmMain = new Frm_Main();
                    frmMain.Show();
                    this.Visible = false;//隐藏登录主窗体
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码有误！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        //打开申请账号窗体
        private void linklblReg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Frm_Register frmRegister = new Frm_Register();//创建申请账号对象
            frmRegister.Show();//显示申请账号窗体
        }
        //实现窗体最小化功能
        private void pboxMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        //实现窗体关闭功能
        private void pboxClose_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {

        }
        //判断是否在密码文本框中按下回车键
        private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r'){//判断是否按下回车键
                pboxLogin_Click(sender, e);//使登录按钮获得鼠标焦点
            }
        }
 
        private void cboxRemember_CheckedChanged(object sender, EventArgs e)
        {
            if (!cboxRemember.Checked) //判断密码是否为未选中状态
            {
                cboxAutoLogin.Checked = false;//自动登录设置为未选中
            }
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            ValidateInput();
            //根据号码查询其密码，记住密码和自动登录字段的值
            string sql = "select Pwd,Remember,AutoLogin from tb_User where ID=" + int.Parse(txtID.Text.Trim()) + "";
            DataSet ds = dataOper.GetDataSet(sql);//查询结果存储到数据集中
            if (ds.Tables[0].Rows.Count > 0)//判断是否存在该用户
            {
                if (Convert.ToInt32(ds.Tables[0].Rows[0][1]) == 1)//判断是否记住密码
                {
                    cboxRemember.Checked = true;//记住密码复选框选中
                    txtPwd.Text = ds.Tables[0].Rows[0][0].ToString();//自动输入密码
                    if (Convert.ToInt32(ds.Tables[0].Rows[0][2]) == 1)//判断是否自动登录
                    {
                        cboxAutoLogin.Checked = true;//自动登录复选框选中
                        pboxLogin_Click(sender, e);//自动登录
                    }
                }
            }
        }

        private void txtID_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}
